package com.hqyj.onlineExamination.modules.exam.dao;

import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import com.hqyj.onlineExamination.modules.exam.entity.Question;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author yun
 * @date 2021-5-25 14:10
 */
@Mapper//把这个DAO交给Spring管理
@Repository//将类识别为Bean
public interface QuestionDao {

    @Select("select * from exam_question where id = #{id}")
    Question selectQuestionById(int id);

    @Insert("insert into exam_question (type, flag, score, content, option_a, option_b, option_c, option_d," +
            "reference_answer, comment, create_date, picture)" +
            "values (#{type}, #{flag}, #{score}, #{content}, #{optionA}, #{optionB}, #{optionC}, #{optionD}," +
            "#{referenceAnswer}, #{comment}, #{createDate}, #{picture})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void addQuestionByQuestion(Question question);

    @Delete("delete from exam_question where id = #{id}")
    void deleteQuestionById(int id);

    @Update("update exam_question set score = #{score}, content = #{content}, option_a = #{optionA}, option_b = #{optionB}," +
            "option_c = #{optionC}, option_d = #{optionD}, reference_answer = #{referenceAnswer}, comment = #{comment}," +
            "create_date = #{createDate}, picture = #{picture} where id = #{id}")
    void updateQuestionByQuestion(Question question);


    @Select("<script>"
            + "select * from exam_question "
            + "<where> "
            + "<if test='keyWord != \"\" and keyWord != null'>"
            + " and (type like '%${keyWord}%' or "
            + " flag like '%${keyWord}%' or "
            + " score like '%${keyWord}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Question> selectAllQuestionInPage(SearchBean searchBean);

    @Select("select type,COUNT(type) AS typeNum from exam_question GROUP BY type")
    @Results(id="typeResult", value={
            @Result(column="typeNum", property="typeNum")}
            )
    List<Question> selectAllQuestionTypeNum();

    @Select("select distinct type from exam_question")
    List<Question> getTypes();

    @Select("select * from exam_question where type = #{type}")
    List<Question> getQuestionsByType(String type);

    @Select("select * from exam_question where type = #{type} and flag = #{flag}")
    List<Question> getQuestionsByTypeAndFlag(@Param("type") String type, @Param("flag") String flag);

    //根据试卷id查询所有的问题
    @Select("SELECT exam_question.* FROM exam_paper_question,exam_question " +
            "WHERE exam_paper_question.question_id = exam_question.id " +
            "AND exam_paper_question.paper_id = #{paperId}")
    List<Question> getAllQuestionsByPaperId(int paperId);
}
